package com.qumitech.visualnovel.service.openplatform.impl;


import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.exception.BusinessException;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.model.common.UrlConstant;
import com.qumitech.visualnovel.pubdef.dto.openplatform.KuaiShouQueryDTO;
import com.qumitech.visualnovel.pubdef.dto.openplatform.OpenPlatFormQueryDTO;
import com.qumitech.visualnovel.pubdef.dto.openplatform.TouTiaoQueryDTO;
import com.qumitech.visualnovel.service.openplatform.OpenPlatformService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

@Service
public class OpenPlatformServiceImpl implements OpenPlatformService {

    private final static String advertTrackUrl = "/adverttrack/activate";
    private static final Logger log = LoggerFactory.getLogger(OpenPlatformServiceImpl.class);

    @Override
    public BaseRsp pushKuaiShouInfoToServer(KuaiShouQueryDTO queryDTO) {
        OpenPlatFormQueryDTO openPlatFormQueryDTO = new OpenPlatFormQueryDTO();
        if (queryDTO.getAndroidDeviceId() != null) {
            openPlatFormQueryDTO.setDeviceId(queryDTO.getAndroidDeviceId());
        }
        if(queryDTO.getAndroidImei() != null){
            openPlatFormQueryDTO.setDeviceImei(queryDTO.getAndroidImei());
        }
        if (queryDTO.getIOSImei() != null){
            openPlatFormQueryDTO.setDeviceImei(queryDTO.getIOSImei());
        }
        if (queryDTO.getMac() != null) {
            openPlatFormQueryDTO.setDeviceMac(queryDTO.getMac());
        }
        openPlatFormQueryDTO.setAdvertType((byte) 1);
        String callBack = queryDTO.getCallbackInfo();
        try {
            String callbackParam = URLDecoder.decode(callBack, "utf8").split("/?callback=")[1];
            openPlatFormQueryDTO.setCallbackParam(callbackParam);
        } catch (UnsupportedEncodingException e) {
            log.error("callBack: " + callBack);
            throw new BusinessException(ResultEnum.OPENPLATFORM_PARAM_ERROR);
        }
        JSONObject req = JSONObject.parseObject(JSONObject.toJSONString(openPlatFormQueryDTO));
        String url = UrlConstant.SERVER_URL + advertTrackUrl;
        log.error("url:" + url);
        log.error("req:" + req);
        //Map<String, String> paramMap = JSONObject.toJavaObject(req, Map.class);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        log.error("body:" + body);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp pushTouTiaoInfoToServer(TouTiaoQueryDTO queryDTO) {
        OpenPlatFormQueryDTO openPlatFormQueryDTO = new OpenPlatFormQueryDTO();
        if (queryDTO.getAndroidDeviceId() != null) {
            openPlatFormQueryDTO.setDeviceId(queryDTO.getAndroidDeviceId());
        }
        if(queryDTO.getAndroidImei() != null){
            openPlatFormQueryDTO.setDeviceImei(queryDTO.getAndroidImei());
        }
        if (queryDTO.getIOSImei() != null){
            openPlatFormQueryDTO.setDeviceImei(queryDTO.getIOSImei());
        }
        if (queryDTO.getMac() != null) {
            openPlatFormQueryDTO.setDeviceMac(queryDTO.getMac());
        }
        openPlatFormQueryDTO.setAdvertType((byte) 2);
        openPlatFormQueryDTO.setCallbackParam(queryDTO.getCallbackInfo());
        JSONObject req = JSONObject.parseObject(JSONObject.toJSONString(openPlatFormQueryDTO));
        String url = UrlConstant.SERVER_URL + advertTrackUrl;
        log.error("url:" + url);
        log.error("req:" + req);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        log.error("body:" + body);
        return JSONObject.parseObject(body, BaseRsp.class);
    }
}
